<?php

/**
 * seo设置 ajax类
 * @package  ajax
 * @subpackage  backend
 * @create by allen.qiang
 */
header('Content-Type:text/html;charset=utf-8');
require 'module/base/systemHandler.class.php';
require 'module/base/goodsHandler.class.php';

class ajax_seo {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //列表
                    $this->seoList();
                    break;

                case 2: //新增或编辑
                    $this->seoAdd();
                    break;

                case 3: //删除
                    $this->seoDel();
                    break;

                case 4: //状态变更
                    $this->changeSeoUseflg();
                    break;

                case 5:  //检查类型是否存在
                    $this->checkExist();
                    break;
				
                case 6://检查详情页商品是否在二级销售商品表中存在
                	$this->checkGoods();
                	break;

            }

        }

    }

    private function checkGoods (){
    	global $G_PGLOB;
    	if (isset($_REQUEST['product_code']) && !empty($_REQUEST['product_code'])){
    		$goodsHandler = new goodsHandler($G_PGLOB->DBCA);
    		$result = $goodsHandler->searchGoodsList($_REQUEST);
    		 
    		if (!$result){
    			$return_arr = array('code'=>400, 'message'=>'没有对应的商品');
    		}else {
    			$return_arr = array('code'=>200, 'message'=>$result[0]['goodsName']);
    		}
    	}else {
    		$return_arr = array('code'=>200, 'message'=>'');
    	}
    	
    	echo json_encode($return_arr);
    }
    
    //seo列表
    public function seoList(){

        global $G_PGLOB;

        //查询
        $systemHandler = new systemHandler( $G_PGLOB->DBCA ) ;
        $result	          = $systemHandler->searchSeoInfo( $_REQUEST ) ;

        //查询结果
        $result_array = array(
            'page'  => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows'  => array()
        );

        if ($result) {
            foreach($result as $data) {

                //类型
                $type = '';
                if( $data['pageType'] == 1 )
                    $type = '首页';
                elseif ( $data['pageType'] == 2 )
                    $type = '列表页';
                elseif ( $data['pageType'] == 3 )
                    $type = '详情页';

                $result_array['rows'][] = array(
                    'id' 	=> $data['uid'].'^'.$data['pageType'],
                    'cell' 	=> array(
                        '<a href="javascript: void(0);" onclick="seolist.edit(\'' . $data['uid'] .'\',' . $data['pageType'] .')">修改</a>&nbsp;' ,
                        $type,
                        $data['pageName'],
                        $data['title'],
                        $data['keywords'],
                        $data['description'],
                        $data['useFlg'] == 1 ? '<a href="javascript: void(0);" onclick="seolist.changeConfirm(\'' . $data['uid'] . '\',' . $data['pageType'] . ', 1) " >启用</a>' :
                            '<a href="javascript: void(0);" onclick="seolist.changeConfirm(\'' . $data['uid'] . '\',' . $data['pageType'] . ', 0) " >禁用</a>'
                    )
                );
            }
        }

        echo json_encode( $result_array );

    }

    /**
     * seo更新或保存
     */
    private function seoAdd() {

        global $G_PGLOB;

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;

        $systemHandler 	 = new systemHandler( $G_PGLOB->DBCA ) ;
        $result_array 		 = $systemHandler->saveSeoInfo( $_REQUEST ) ;

        echo json_encode($result_array);

//        error_log( print_r($_REQUEST,true) );
//        echo json_encode( array('code'=>200 , 'message'=>'ok') );

    }

    /**
     * 删除seo
     *
     */
    private function seoDel () {

        global $G_PGLOB;

        /**接收参数
         *
         * $_REQUEST[delArr] => Array(
        [0] => 19-1
        [1] => 18-2
        [2] => 17-1
        )
         *这是由要删除的 uid-pageType 组成的数组
         */
        $delArr = isset($_REQUEST['delArr'])? $_REQUEST['delArr'] : '';

        if( empty($delArr) ){
            echo json_encode( array('message'=>'请选择要删除的seo' , 'code'=>400 ) );
            return;
        }

        //传参数组
        $params = array();

        //转化参数数组
        foreach( $delArr as $value ){

            //分解
            $getArr = explode('^',$value);

            $params[] = $getArr;

        }

        $systemHandler = new systemHandler( $G_PGLOB->DBCA ) ;
        $result_array  = $systemHandler->delSeoInfo( $params ) ;

        echo json_encode($result_array);

    }

    /**
     * 状态变更
     */
    private function changeSeoUseflg() {

        global $G_PGLOB;

        $pageType = $_REQUEST['pageType'];
        $uid      = $_REQUEST['uid'];
        $flg   = $_REQUEST['flg'];

        $seo_obj = new entity('sys_seo');
        $seo_obj->uid = $uid;
        $seo_obj->pageType = $pageType;


        if( !$G_PGLOB->DBCA->fetch($seo_obj) ){

            echo json_encode( array('message'=>'没有找到信息' , 'code'=>404 ) );
            return;

        }else{

            $useFlg = $seo_obj->useFlg;

            if( $flg == 1 && $useFlg == 0){
                echo json_encode( array('message'=>'该SEO已处于<b>禁用</b>状态<br>请重新刷新页面' , 'code'=>400 ) );
                return;
            }

            if( $flg == 0 && $useFlg == 1){
                echo json_encode( array('message'=>'该SEO已处于<b>启用</b>状态<br>请重新刷新页面' , 'code'=>400 ) );
                return;
            }

            if( $useFlg == 0 ){
                $seo_obj->useFlg = 1;
            }else{
                $seo_obj->useFlg = 0;
            }

        }

        try{

            $G_PGLOB->DBCA->save( $seo_obj );

            if( $useFlg == 0 ){
                echo json_encode( array('message'=>'已启用' , 'code'=>200 ) );
                return;
            }else{
                echo json_encode( array('message'=>'已禁用' , 'code'=>200 ) );
                return;
            }


        }catch(Exception $e){

            error_log( $e->getMessage() );
            echo json_encode( array('message'=>'系统错误，请与管理员联系' , 'code'=>500 ) );
            return;
        }

    }

    //检查是否存在
    function checkExist(){

        global $G_PGLOB;

        //接收参数
        $pageType = $_REQUEST['pageType'];

        if( in_array($pageType,array(1,3)) ){
            $check_exist  = 'SELECT * FROM sys_seo WHERE pageType=? AND uid=?';
            $check_result = $G_PGLOB->DBCA->stmt_query($check_exist,'ii',array($pageType,0));
        }else{
            echo json_encode( array('code'=>200 , 'message'=>'此类型可以添加') );
            return;
        }


        if( $check_result ){
            echo json_encode( array('code'=>400 , 'message'=>'此类型已存在') );
        }else{
            echo json_encode( array('code'=>200 , 'message'=>'此类型可以添加') );
        }

    }

}